package cn.bdqn.controller;

import cn.bdqn.domain.Cargo;
import cn.bdqn.service.CargoService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 货物控制层
 */
@RestController
public class CargoController {

    @Autowired
    private CargoService cargoService;

    /**
     * 根据主键id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/cargo/queryByPrimaryKey/{id}")
    public Result queryByPrimaryKey(@PathVariable("id") Integer id) {
        Cargo cargo = cargoService.queryByPrimaryKey(id);
        return ResultGenerator.genSuccessResult(cargo);
    }

    /**
     * 多条件查询   分页
     * @param cargo
     * @return
     */
    @GetMapping(value = "/cargo/queryByCargo/cargo/pageNum/{pageNum}/pageAmount/{pageAmount}")
    public Result queryByCargo(@RequestBody Cargo cargo, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageAmount") Integer pageAmount){

        try {
            Page page = cargoService.queryByCargo(cargo,pageNum,pageAmount);

            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());

        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 新增
     * @param cargo
     */
    @RequestMapping(value = "/cargo/insert/cargo")
    public Result insert(@RequestBody Cargo cargo){

        try {
            cargoService.insert(cargo);
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("新增失败");
        }
    }

    /**
     * 选择更新
     * @param cargo
     */
    @RequestMapping(value = "/cargo/updateByPrimaryKeySelective/cargo")
    public Result updateByPrimaryKeySelective(@RequestBody Cargo cargo){

        try {
            cargoService.updateByPrimaryKeySelective(cargo);
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("更新失败");
        }
    }

}
